import { reactive } from 'vue'
import { defineStore } from 'pinia'

export const useAppStore = defineStore('app', () => {
  const app = reactive({
    version: 101,
    currentAppVersion: 100,
    loading: false
  })

  const upList = reactive({
    baBaNongChang: {
      type: 'baBaNongChang',
      version: 1,
      url: 'https://raw.githubusercontent.com/Le-u/Download/master/script/baBaNongChang.js'
    },
    cancelVidoe: {
      type: 'cancelVidoe',
      version: 1,
      url: 'https://raw.githubusercontent.com/Le-u/Download/master/script/cancelVidoe.js'
    },
    deleComments: {
      type: 'deleComments',
      version: 1,
      url: 'https://raw.githubusercontent.com/Le-u/Download/master/script/deleComments.js'
    },
    autoComments: {
      type: 'autoComments',
      version: 1,
      url: 'https://raw.githubusercontent.com/Le-u/Download/master/script/autoComments.js'
    },
    getPosition: {
      type: 'getPosition',
      version: 1,
      url: 'https://raw.githubusercontent.com/Le-u/Download/master/script/getPosition.js'
    },
  })

  const setVersion = (version) => {
    app.currentAppVersion = version
  }



  try {
    $autox.registerHandler("sendSuccess", () => {
      Snackbar.success("更新成功")
      app.loading = false
    });
  } catch (err) {
  }

  const isVersion = (version) => {
    if (version < app.version) {
      return true
    } else {
      return false
    }

  }

  const toastLog = (data) => {
    sendAndroid('AndroidToastLog', data)
  }

  const sendAndroid = (name, data = '', cd = () => { }) => {
    try {
      $autox.callHandler(name, data, (data) => {
        cd(data)
        // if (data) toastLog(data)
      });
    } catch (err) { }
  }


  const upScript = (param) => {
    let params = JSON.stringify(upList[param])
    sendAndroid('AndroidUpScript', params, (data) => {
      let bool = parseInt(data)
      if (bool) {
        app.loading = true
        Snackbar.loading('正在更新,请勿做其他操作')
      } else {
        Snackbar('已是最新')
      }
    })
  }


  return { app, isVersion, sendAndroid, setVersion, upScript }
})